import Vue from 'vue';
import Vuex from 'vuex';
import {
	router,
	constantRouterMap,
	asyncRouterMap
} from 'router';

Vue.use(Vuex)
export default new Vuex.Store({
	state: {
		menus: [],
		routes: [],
		addRoutes: [],
		skin: "",
		skinColor: {
			blue: "#409eff",
			green: "#009688",
			black: "#303133",
			grey: "#c0c4cc",
			darkblue: "#334157",
			purple: "#926dde",
		}
	},
	mutations: {
		setMenus(state, menus) {
			state.menus = menus;
		},
		setRouters(state, addRoutes) {
			state.routes = constantRouterMap;
			state.addRoutes = addRoutes;
			state.routes = constantRouterMap.concat(addRoutes);
		},
		setSkin(state, skin) {
			state.skin = skin;
		}
	},
	actions: {
		setSkin(context, skin) {
			context.commit("setSkin", skin);
		},
		initRouter(context, menus) {
			return new Promise(resolve => {
				const result = [];
				const children = [];
				result.push({
					path: '/',
					component: () => import("views/index.vue"),
					children,
				})
				for (let menu of menus) {
					if (menu.urlType == 2) {
						children.push({
							path: menu.url,
							component: () => import("components/iframe/myFrame.vue"),
							meta: {
								pathUrl: menu.pathUrl,
							}
						});
					}
				}
				for (let child of asyncRouterMap[0].children) {
					for (let menu of menus) {
						if (menu.urlType == 1 && menu.url == child.path) {
							children.push(child);
						}
					}
				}
				children.push({
					path: '*',
					component: () => import("views/404.vue")
				})
				context.commit("setMenus", menus);
				context.commit("setRouters", result);
				resolve();
			});
		},
	},
	getters: {
		getMenus: state => {
			return state.menus;
		},
		getRoutes: state => {
			return state.routes;
		},
		getAddRoutes: state => {
			return state.addRoutes;
		},
		getSkin: state => {
			return state.skin;
		},
		getSkinColor: state => {
			return state.skinColor;
		},
	},
	modules: {}
})
